
package com.phevos.common.dbpool;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * ����ݿ��l�ӳأ�ÿ����ݿⱣ��һ��l��
 * <p>
 * Title: ��Դ��ݼ��ϵͳ
 * </p>
 * <p>
 * Description: ��Դ��ݼ��ϵͳ
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company: ������j���¿Ƽ����޹�˾
 * </p>
 * 
 * @author tangli
 * @version 1.3
 */
public abstract class MultidbConnectionPool
{
    private Hashtable pool = new Hashtable();

    public MultidbConnectionPool()
    {
    }

    public synchronized Connection getConnection(String url, String usrname, String pwd)
            throws SQLException, IOException
    {
        BasicDataSource ds = (BasicDataSource)pool.get(url);
        if (ds == null)
        {
            ds = newDataSource(url, usrname, pwd);
            pool.put(url, ds);
        }
        Connection conn = ds.getConnection();
        if (conn == null)
        {
            throw new SQLException("no idle connection");
        }
        else
        {
            return conn;
        }
    }

    public synchronized Connection getConnection(String svrName) throws SQLException, IOException
    {
        BasicDataSource ds = (BasicDataSource)pool.get(svrName);
        if (ds == null)
        {
            ds = newDataSource(svrName);
            pool.put(svrName, ds);
        }
        Connection conn = ds.getConnection();
        if (conn == null)
        {
            throw new SQLException("no idle connection");
        }
        else
        {
            return conn;
        }
    }

    public void returnConnection(Connection conn)
    {
        if (conn != null)
        {
            try
            {
                conn.close();
            }
            catch (SQLException ex)
            {
            }
        }
    }

    protected abstract BasicDataSource newDataSource(String svrName) throws SQLException,
            IOException;

    protected abstract BasicDataSource newDataSource(String url, String usrname, String pwd)
            throws SQLException, IOException;
}
